

import (TaskInput) in "task/input";

string s;
static int abcd;
var ta;


//自动扫描并绑定指令
void initCmds(){
    var x = getDeepDir("cmds");
    for(int i;i<x.size();i++){
        println("执行了吗"+i);
        var v = x[i].path;
        println("当前文件是--->"+v);

        string clsName = rmSuffix(v);
        println("类名是:"+clsName);

        int len = clsName.lastIndexOf("/");
        string cmd = clsName.subString(len+1,strlen(clsName));
        println(cmd);
        TaskInput::cmdObjs.put(cmd,new( clsName ));
    }
    println("数据查看");
    println(TaskInput::cmdObjs);
}
//自动载入 常驻文件
void initDaemons(){
    var x = getDeepDir("adm/");
    for(int i;i<x.size();i++){
        println("执行了吗"+i);
        var v = x[i].path;
        //"当前daemons文件是--->"+v
         println(fmt("load daemons %s",v));
        if( strsrch(v,"simefun") > 0 ) {
            println("数据不对"+v);
            continue;
        }

        string clsName = rmSuffix(v);
        println("类名是:"+clsName);
        new( clsName );

    }

}



//https://wstool.js.org/ 在线websocket 测试工具
void createWssServer(){

   var wssSvr = createNet("wss","9909");

    wssSvr.on("open",(id)->{
        //发出要求登录的信息
        //wssSvr 从createWssServer 逃逸了...
//       wssSvr.send(id,"你要先登录啊");
        println("open了奥");
    });

    wssSvr.on("message",(nid,msg)->{
             var wssSvr1=wssSvr;

            var loginUser = filterMap(objects(),(k,v)->{
                //是用户 且 agentId已绑定 就是登录了的
                if(   userp(v) && v.query("netAgentId") == nid  ){
                    println("找到了------------------------>");
                    return true;
                }
            });

            if(!loginUser){
                 wssSvr.send(nid,"你要先输入指令登录啊!");
                var instr = msg.split(" ");
                 //跳过第一位，取多少位
                 //argv 是参数内容
                (uname,passwd) = instr;
                if( uname!="far" || passwd!="123"){
                    wssSvr.send(nid,"账号或密码不正确!");
                    return;
                }
                wssSvr.send(nid,"登录成功了奥!");
                var user = new("obj/user");
                user.set("id",hashCode(user));
                user.set("netAgentId",nid);
                user.set("uname",uname);
                user.set("passwd",passwd);
                user.set("money",300000);
                user.set("netChannel",wssSvr);
                user.move("d/chengdu/zygc");

                println("移动到了"+baseName(environment(user)));
                //注册模拟游戏
//                D("adm/daemons/citygame").regGame(user);
                return;
            }

           (cmd,argv) = parseCommand(msg);

            println("当前的指令是-->" + cmd);

             if( !TaskInput::cmdObjs.has(cmd) ){
                 wssSvr.send(loginUser.query("netAgentId"),"什么？？")
                 return;
             }
             TaskInput::cmdObjs[cmd].main(loginUser,argv);
    });


    wssSvr.on("close",(id)->{
        println("客户端关闭了");
    });

    wssSvr.start();

}



static int test(){
    println("111");
}

void main(string* args){

    var mapVal = jsonStrToMap(toJsonStr(this));
    println(toJsonStr(mapVal));
    if( funcExists(this,"initCmds") ){
        println("initCmds");
    }
    //初始化daemons
    initDaemons();
    //初始化指令
     initCmds();
//       D("adm/daemons/roundGame").loadExcelData();
//    var testUser = new("obj/user");
//    testUser.set("land/x/d",100);
////    testUser.query("land/x");
//    testUser.query("land/x/d");
//
//    println(testUser.query("land/x"));

//    if( 1 in (1,2,3) ){
//       println("是得，在里面奥");
//    }

//    var testWhen = when(a=1){
//        "x" => "11";
//        !in (1,2,3))=>"浑浑噩噩";//生成指令 IN_SIN
//        !in 1..20 =>"111";//生成指令 IN_AREA
//       testUser && 1=>"鹅鹅鹅饿";
//       !a =>println(a);
//    };






//varDump(testUser.query("land/x"));
//varDump(testUser.query("land/x/d"));

//D("adm/daemons/landd").test();
//     println(varDump(D("adm/daemons/landd")));


//    var runner = new("task/input");
//    var task = createThread(runner,"run");
//    task.start();


    createWssServer();



  var prevTime;
  for(;;){

     var curTime = time();

     //prevTime 有问题，需要初始化
     if(  (curTime-prevTime) / 1000 % 60  >= 1 ){

        prevTime = time();
        var allKeys = keys(TaskInput::allObjs);
        for(int i=0; i < allKeys.size(); i++){
            var obj = TaskInput::allObjs[allKeys[i]];
            if( funcExists(obj,"heartbeat") ){
              obj.heartbeat();
            }
        }
     }

  }


}



